#! /usr/bin/python

#False expression: 1 None,2 0,3 "",4 [],5 {},6 ()
import module_test
def testif():
    x = 3
    if (x > 0):
        print "x is positive"
    elif x==0:
        print "x is zero"
    else:
        print "x is negative"

testif()

def testfor():
    for x in "abc": print x
    for y in [1,2,3]:
        print y
        if y == 100:
            break
        y = y+1
    else:
        print "100 is not found"

    print y
    D = {"one":1, "two":2, "three":3}
    for x in D:
        D[x] += 10
        print "D[",x,"]=",D[x]
        if x == "two":
            break
    print D
    a=[1,2,3,4,5]
    for x in range(len(a)):
        a[x] = a[x] + 1
    print a
testfor()

def testwhile():
    i = 0
    while i < 3:
        print i; i = i+1
    else:
        print i, "is 3"

testwhile()

def add(a,b):
    return a+b

print add(1, 2)
print add("abc", "hello")

def myjoin(str, sep=','):
    return sep.join(str)

print myjoin(["abc", "124", "hahah"])
#print myjoin(sep=';', ["abc", "124", "hahah"])
#variable parameter list
def printf(format, *arg):
    print type(arg)
    print format%arg
printf ("%s is %d", "renwang", 1)

def printf(format, **keyword):
    print type(keyword)
    for k in keyword:
        print "keyword[%s] is %d"%(k, keyword[k])

#printf("oc", {"one":1, "two":2, "three":3})
printf("hah", one=1, two=2, three=3)

def printf(fixed, optional=1, *arg, **keyword):
    print  "fixed is ", fixed
    print  "optional is ", optional
    print  "arbitrary argument is ", arg
    print  "keyword is ", keyword

printf(2,1,"hello", 23, 234, "af", "ab", "12", abc=234)
#test the comment
def testfuncdoc():
    """
    just demonstrate the use of __doc__
    """
    pass

testfuncdoc()
print testfuncdoc.__doc__
print "".join.__doc__
print range.__doc__

#lambda expression
f = lambda   a,b:a+b
print f(1, 2)
print f("ab", "ren")

def funcinc(a):
    return lambda x:x+a
print funcinc(1)(1)
print funcinc(10)(100)
print funcinc(432432)(-123123)

#test func scope
a = "1bc"
def testfuncscope():
    #a = 1
    print a
    global a
    print a
testfuncscope()
print testfuncscope
#nested function
def outfunc(a, b):
    def innerfunc(x, y):
        return x+y
    return innerfunc(a, b)

print outfunc("1", "abc")

print module_test.__doc__
print module_test.age
module_test.age = 111
print module_test.age
print module_test.sayHello
module_test.sayHello()
sayHello = module_test.sayHello
sayHello()

from module_test import age, sayHello
print age
print sayHello

import sys
print sys.path

import packagetest.module_test
packagetest.module_test.sayHello()

print __name__
def testnamespace():
    a = 1;
    b = "abkc"
    print locals()
    print globals()

testnamespace()
print a
del a

str = "string"
print str
print dir(str)
print hasattr(str, "hha")
print hasattr(str, "__str__")

